package com.shrcl.engineerprocess.web.client.app

import com.shrcl.engineerprocess.web.client.names.ObjectNameService
import com.google.inject.Inject
import com.google.inject.name.Named
import com.google.inject.name.Names
import com.uniquesoft.gwt.client.common.RuntimeGinjector
import com.uniquesoft.gwt.client.common.command.ICommandEventsListener
import com.uniquesoft.gwt.client.common.gin.UIDLBaseGinModule
import com.uniquesoft.gwt.client.common.name.IObjectNameProvider
import edu.fudan.langlab.gxt.client.pager.Pager2
//import edu.fudan.langlab.uidl.domain.app.client.ActiveMenu
//import edu.fudan.langlab.uidl.domain.app.client.IWorkbenchMenuContribution
import edu.fudan.langlab.uidl.domain.app.client.ModuleInitializer
import edu.fudan.langlab.uidl.domain.app.client.WorkbenchManager
import edu.fudan.langlab.uidl.domain.app.client.WorkbenchManagerImpl
import edu.fudan.langlab.uidl.domain.app.client.login.FudanmedLoginView
import edu.fudan.langlab.uidl.domain.app.client.login.LoginPresenter
import edu.fudan.langlab.uidl.domain.app.client.login.LoginPresenterView
import edu.fudan.langlab.uidl.domain.app.client.task.TaskRegister
import edu.fudan.langlab.uidl.domain.log.client.web.CommandEventsListener
//import org.reflections.Reflections

class MainModule extends UIDLBaseGinModule {
    override protected configure() {
    
		bind(typeof(RuntimeGinjector)).to(typeof(MainGinjector));
		bind(typeof(IObjectNameProvider))
				.to(typeof(ObjectNameService));
		bindPresenter(typeof(edu.fudan.langlab.uidl.domain.app.client.login.LoginPresenter), 
				typeof(edu.fudan.langlab.uidl.domain.app.client.login.LoginPresenterView),
				typeof(edu.fudan.langlab.uidl.domain.app.client.login.FudanmedLoginView));
				
		bind(typeof(WorkbenchManager)).to(typeof(WorkbenchManagerImpl))
				.asEagerSingleton();
			
		bind(typeof(ICommandEventsListener)).to(typeof(edu.fudan.langlab.uidl.domain.log.client.web.CommandEventsListener)).asEagerSingleton
		bind(typeof(ModuleInitializer)).to(typeof(ModuleInitializerImpl));
		bind(typeof(UIWorkbenchPartRegister));
		bind(typeof(Pager2))
		bind(typeof(edu.fudan.langlab.uidl.domain.app.client.task.TaskRegister)).asEagerSingleton	

//        val Reflections reflections = new Reflections("com.shrcl.engineerprocess.web");
//        val namedMenuContributions = reflections.getSubTypesOf(typeof(IWorkbenchMenuContribution)).filter[
//            it.getAnnotation(typeof(ActiveMenu)) != null
//        ].toMap[it.getAnnotation(typeof(ActiveMenu)).value]
//        
//        IterableExtensions.forEach(
//        	getNamedInjectedFields(typeof(UIWorkbenchPartRegister), typeof(IWorkbenchMenuContribution)))[
//            val impl = namedMenuContributions.get(it.key)
//            println(" >> bind " + it.key + " to " + impl); 
//            if(impl!=null)
//                bind(typeof(IWorkbenchMenuContribution)).annotatedWith(Names::named(it.key)).to(impl)
//        ]

    }

    def getNamedInjectedFields(Class<?> clazz, Class<?> fieldType){
        clazz.declaredFields.filter[
            it.getAnnotation(typeof(Inject)) != null && 
            it.getAnnotation(typeof(Named)) != null &&
            it.type.equals(fieldType)
        ].^map[{ it.getAnnotation(typeof(Named)).value -> it}]
    }
    
}
